package vrp;

import java.io.IOException;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import vrp.Product;
import vrp.PMF;

public class ProductAddServlet extends HttpServlet {

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {

    	long parterID = Integer.parseInt(req.getParameter("partner"));
        Key partnerKey = KeyFactory.createKey(Partner.class.getSimpleName(), parterID);
        String name = req.getParameter("name");
        double price = Float.parseFloat(req.getParameter("price"));
        double weight = Float.parseFloat(req.getParameter("weight"));
        int inventory = Integer.parseInt(req.getParameter("inventory"));

        Product product = new Product(partnerKey, name, price, weight, inventory);
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(product);
        } finally {
            pm.close();
        }
        resp.sendRedirect("/products.jsp");
    }
}
